// pages/list/review.js
const db=wx.cloud.database()
const question_db=db.collection('question')
import Toast from '@vant/weapp/toast/toast';
var util = require('../../utils/utils.js');
Page({
  review(){
    wx.navigateTo({
      url: '../list/index',
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    t:[],
    QR:[],
    v:"v1.0",
    tle:"未填写"
  },
  shujv(){
    for (let i = 1; i < this.data.QR.length+1; i++){
     this.data.t.push(i)
     this.setData({
       t:this.data.t,
       l:this.data.t.length
     })
    }
  },
  continue(){
    if(this.data.l<10){
    wx.navigateTo({
      url: '../make/index',
    })
  }if(this.data.l==10){wx.showToast({
    title: '题目已到上限',
    icon:'none',
  })}
  },

  dj2(e){
    var i = e.currentTarget.id
    wx.navigateTo({
      url: '../make/check?id='+i,
    })
  },
  v(e){
    var v =e.detail.value
    this.setData({
      v:v,
      vl:v.length
    })
    
  },
  tle(e){
    var tle =e.detail.value
    this.setData({
      tle:tle,
    })
  },
  submit(){
    if(this.data.l<10){
      wx.showToast({
        title: '未满10道题',
        icon:"error"
      })
    }
    else{
    question_db.add({
      data: {
      _id:this.data.v,
       title:this.data.tle,
       month:this.data.time.substring(5,7),
       year:this.data.time.substring(0,4),
       question:this.data.make,
      },
      success: res=>{
        const toast = Toast.loading({
          duration: 0, // 持续展示 toast
          forbidClick: true,
          message: '提交成功!',
          selector: '#custom-selector',
        });
        let second = 3;
        const timer = setInterval(() => {
          second--;
          if (second) {
            toast.setData({
              message: ` ${second} 秒后返回`,
            });
          } else {
            clearInterval(timer);
            Toast.clear();
            wx.navigateTo({
              url: '../index/index2',
            })
          }
        }, 1000);
        wx.removeStorageSync('question')
      },
      fail: err => {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: '网络不给力....'
        })
      }
    })
    }
  },
  get_time(){
    var get_time=util.formatTime2(new Date())
    this.setData({
    time:get_time.substring(0,10)
    })
    console.log(this.data.time)
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_time()
   
    var getQR=wx.getStorageSync('question')
    this.setData({
      QR:getQR
    })
    this.shujv()
    console.log(this.data.QR)
    var make=[{"regular":this.data.QR}]
    this.setData({
      make:make
    })
    console.log(this.data.make)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})